﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms;
using System.IO;

namespace OpenFileDialog和SaveFileDialog的使用
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "文本|*.txt|图片|*.jpg|所有|*.*";
            DialogResult dr = openFileDialog1.ShowDialog();
            string fileName = openFileDialog1.FileName;
            if(dr == DialogResult.OK && !string.IsNullOrEmpty(fileName))
            {
                StreamReader sr = new StreamReader(fileName);
                textBox1.Text = sr.ReadToEnd();
                sr.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult dr = saveFileDialog1.ShowDialog();
            string fileName = saveFileDialog1.FileName;
            if(dr == System.Windows.Forms.DialogResult.OK && !string.IsNullOrEmpty(fileName))
            {
                StreamWriter sw = new StreamWriter(fileName, true, Encoding.UTF8);
                sw.Write(textBox1.Text);
                sw.Close();
                MessageBox.Show("保存成功！", "提示");
            }
        }
    }
}
